import 'package:example/app/controller/favourite_controller.dart';
import 'package:example/app/data/model/article_bean.dart';
import 'package:example/app/utils/favourite_helper.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_base_framework/flutter_base_framework.dart';
import 'package:flutter/material.dart';

import 'animated_provider.dart';

class ArticleFavouriteWidget extends StatelessWidget {
  final ArticleBean articleBean;

  const ArticleFavouriteWidget({
    Key? key,
    required this.articleBean,
  }) : super(key: key);


  @override
  Widget build(BuildContext context) {
    FavouriteController controller = Get.put(FavouriteController(), tag: articleBean.id.toString());
    return GetBuilder<FavouriteController>(
      init: controller,
      tag: articleBean.id.toString(),
      builder: (_) {
        return GestureDetector(
          behavior: HitTestBehavior.opaque, //否则padding的区域点击无效
          onTap: () async {
            if (!_.busy) {
              addFavourites(context,
                  article: articleBean);
            }
          },
          child: Padding(
              padding: const EdgeInsets.fromLTRB(20, 20, 20, 10),
              child: ScaleAnimatedSwitcher(
                  child: _.busy
                      ? SizedBox(
                      height: 24,
                      width: 24,
                      child: CupertinoActivityIndicator(radius: 5))
                      : Icon(
                      articleBean.collect == true
                          ? Icons.favorite
                          : Icons.favorite_border,
                      color: Colors.redAccent[100]))),
        );
      },
    );
  }
}
